/**
 * 
 */
package com.zxc.curator;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.curator.RetryPolicy;
import org.apache.curator.RetrySleeper;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.CuratorWatcher;
import org.apache.curator.framework.recipes.atomic.DistributedAtomicInteger;
import org.apache.curator.framework.recipes.barriers.DistributedBarrier;
import org.apache.curator.framework.recipes.barriers.DistributedDoubleBarrier;
import org.apache.curator.framework.recipes.leader.LeaderLatch;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.retry.RetryForever;
import org.apache.curator.utils.CloseableUtils;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.data.Stat;

import com.google.common.collect.Lists;

/**
 * @author Administrator
 *
 */
public class CuratorClient {
	
	private static String PATH = "/configure";

	public static void main(String[] args) {
		CuratorFramework client = CuratorFrameworkFactory.builder()
				.connectString("192.168.18.100:2181")  
		        .sessionTimeoutMs(30000)  
		        .connectionTimeoutMs(30000)  
		        .canBeReadOnly(false)  
		        .retryPolicy(new ExponentialBackoffRetry(1000, Integer.MAX_VALUE))  
		        //.namespace("shijie99.com")  
		        //.defaultData(null)  
		        .build();  
		try {  
            client.start();  
  
//            client.create().forPath(PATH, "I love messi".getBytes());  
//            
           // System.out.println(client.create().forPath("/configureMonitor"));
//            client.create().forPath(PATH+"/autoticket");
//            client.create().forPath(PATH+"/autoticket/test");
//            client.create().forPath(PATH+"/autoticket/test/1.0");
            
//              String str = client.create().creatingParentsIfNeeded().forPath(PATH+"/autoticket/test/1.0","I love messi".getBytes());
//              System.out.println(str);
              
            //client.setData().forPath(PATH+"/autoticket/test/1.0", "I love messi".getBytes());
            
//            client.delete().guaranteed().deletingChildrenIfNeeded().forPath("/zookeeper");
//            
//            Stat stat = client.checkExists().usingWatcher(new CuratorWatcher() {
//				
//				@Override
//				public void process(WatchedEvent event) throws Exception {
//					System.out.println(event.getPath());
//				}
//			}).forPath(PATH+"/autoticket/test/1.0");
//            if(stat == null){
//            	System.out.println(PATH+"/autoticket/test/1.0    路径不存在");
//            	return;
//            }
//            System.out.println(stat.toString());
//            byte[] bs = client.getData().forPath(PATH+"/autoticket/test/1.0");  
//            System.out.println("新建的节点，data为:" + new String(bs));  
            
//           List<String> pathList = client.getChildren().forPath("/");
//           System.out.println(pathList.toString());
            
  
//            client.setData().forPath(PATH, "I love football".getBytes());  
//  
//            // 由于是在background模式下获取的data，此时的bs可能为null  
//            byte[] bs2 = client.getData().watched().inBackground().forPath(PATH);  
//            System.out.println("修改后的data为" + new String(bs2 != null ? bs2 : new byte[0]));  
//  
//            client.delete().forPath(PATH);  
//            Stat stat = client.checkExists().forPath(PATH);  
//  
//            // Stat就是对zonde所有属性的一个映射， stat=null表示节点不存在！  
//            System.out.println(stat);  
            client.delete().deletingChildrenIfNeeded().forPath("/jodis");
            
            
    			
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            CloseableUtils.closeQuietly(client);  
        }
		
	}
}
